//
//  CollectViewController.m
//  TextApplication
//
//  Created by dllo on 16/9/17.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "CollectViewController.h"
#import "TextView.h"
#import "CollectCollectionViewCell.h"
#import "TAMacro.h"

#import "ImageUrl.h"
#import "LikeDetailViewController.h"
#import "ImageUrl.h"
#import "SQLiteDataBaseManager.h"


@interface CollectViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UIAlertViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray <ImageUrl *>*dataArray;
@property (nonatomic, strong) SQLiteDataBaseManager *dataBaseManager;

@end

@implementation CollectViewController

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    // 当没有收藏时会出现提示框
    [self createAlertView];

      // 可以在收藏页面进行取消收藏
    [self createData];
    [self.collectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    self.dataBaseManager = [SQLiteDataBaseManager sharedManager];
    [self.dataBaseManager openSQLite3];
    self.dataArray = [NSMutableArray array];

    [self createLeftButton];
    [self createCollectionView];
    [self createData];
    
}

- (void)createAlertView {
    self.dataArray = [self.dataBaseManager selectAll];
    if (self.dataArray.count == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"您还没有收藏哦" message:nil delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [alertView show];
    }
}
// alertView消失后会执行的方法
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    [self.navigationController popToRootViewControllerAnimated:NO];
}

- (void)createData {
    self.dataArray = [_dataBaseManager selectAll];
}

- (void)createLeftButton {
    self.title = @"我的收藏";
    UIButton *left = [TextView createButtonWithImage:[UIImage imageNamed:@"10"]];
    [left addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:left];
}

- (void)backClick:(UIButton *)button {
    [self.navigationController popToRootViewControllerAnimated:NO];

}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 44) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[CollectCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.imageStr = self.dataArray[indexPath.row].imageStr;
    NSLog(@"%@", self.dataArray[indexPath.row].imageStr);

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LikeDetailViewController *likeVC = [[LikeDetailViewController alloc] init];
    likeVC.goodID = self.dataArray[indexPath.row].image_id;
    [self.navigationController pushViewController:likeVC animated:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
